/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 2 -*- */
/**
* @headerfile socket.hpp
* 
* Header for easy comunication through sockets.
*
* @author Miguel Ángel Vílchez López 2008 <sacra@openmatica.es> 
*
* Codigo is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* 
* Codigo is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License along
* with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef _MOKARDU_SOCKET_HPP_
#define _MOKARDU_SOCKET_HPP_

#include <netinet/in.h>
#include <netdb.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include "debug.hpp"

/**
* @class CMSocket
* @brief Comunication through sockets.
*/

class CMSocket
{

	private:
		
		/**
		* The log class.
		*/
		CMDebug debug;
		
		/**
		* The identifier of socket.
		*/
		int id;

		/**
		* The ip of socket.
		*/
		char* ip;

		/**
		* The TCP port.
		*/
		unsigned int port;
		
		/**
		* The buffer size.
		*/
		unsigned int buffer_size;
				
		/**
		* The socket address size.
		*/	
		socklen_t address_size;
		
		/**
		* The struct of the socket.
		*/
		struct sockaddr_in struct_socket;


	public:

		/**
		* Constructor.
		*
		* @param[in] port The port of socket tcp.
		* @param[in] ip The ip of socket tcp.
		* @param[in] buffer The buffer size.
		* @param[in] debug If the debug is activated.
		* @param[in] filename The file for logging.
		*/
		CMSocket(unsigned int port, const char* ip, unsigned int buffer,bool debug,const char* filename);
	
		/**
		* Deconstructor.
		*/
		~CMSocket();
	
		/**
		* Methods commons to server and client.
		*/
		void init();
		
		/**
		* Create a new socket.
		*
		* @return Returns 0 if succesfull, -1 if not.
		*/
		int serverTcp();

                /**
                * Accept new connections.
                *
                * @return The id_accept for read data.
                */
                int serverAccept();

		/**
		* Read from a socket client.
		*
		* @param[out] data The variable to fill with the data received.
                * @param[in] id_accept The accept id from serverAccept.
		*/
		int serverRead(char* data, int id_accept);

                /**
		* Write to a socket client.
		*
		* @param[out] data The data to send.
                * @param[in] id_accept The accept id from serverAccept.
		*/
                int serverWrite(char* data, int id_accept);
		
		/**
		* Connect to a server.
		*
		* @return Returns 0 if succesfull, -1 if not.
		*/
		int clientTcp();
		
		/**
		* Send data to server.
                *
		* @param[out] data The data to send.
		* @return Returns 0 if succesfull, -1 if not.
		*/
		int clientWrite(char *data);

                /**
		* Receive data from server.
                *
		* @param[out] data The variable to fill with the data received.
		* @return Returns 0 if succesfull, -1 if not.
		*/
                int clientRead(char* data);

};

#endif // _MOKARDU_SOCKET_HPP_
